from PyQt5.QtWidgets import QWidget, QToolTip, QPushButton, QFileDialog, QLineEdit, QLabel
from PyQt5.QtGui import QIcon,QFont
from PyQt5.QtCore import pyqtSlot, Qt
from parsers.FileData import FileData
import os

class QtIconInstance(QWidget):

    def __init__(self):
        super().__init__()
        self.cwd = os.getcwd()
        self.file_path = ""
        self.initUI()



    def databack(self,cmd,content):
        '''
        '''
        if cmd == 0:
            self.file_type_label.setText(content)

    def setBrowerPath(self):
        # file_path = QFileDialog.getExistingDirectory(self,"浏览")
        self.file_path,_ = QFileDialog.getOpenFileName(self,
                                    "选取文件",
                                    self.cwd, # 起始路径
                                    "All Files (*);;Text Files (*.txt)")
        self.lineEdit.setText(self.file_path)

    def initUI(self):
        QToolTip.setFont(QFont('SansSerif', 10))
        self.setToolTip('This is a <b>QWidget</b> widget')
        # 文件按钮
        btn = QPushButton('文件路径', self)
        btn.setToolTip('This is a <b>QPushButton</b> widget')
        btn.resize(btn.sizeHint())
        btn.move(10, 10)
        """按钮与鼠标点击事件相关联"""
        btn.clicked.connect(self.on_file_click)

        self.setGeometry(400, 400, 400, 320)
        self.setWindowTitle('Parser')
        self.setWindowIcon(QIcon('res\dog.jpg'))
        # 输入框
        self.lineEdit = QLineEdit(self)
        # self.lineEdit.textChanged.connect(self.on_text_change)
        self.lineEdit.move(100, 10)

        # 确认按钮
        ack_btn = QPushButton('确认', self)
        ack_btn.setToolTip('This is a <b>确认</b> button')
        ack_btn.resize(btn.sizeHint())
        ack_btn.move(250, 10)
        ack_btn.clicked.connect(self.on_ack_click)

        # 文件类型
        self.file_type_label = QLabel(self)
        self.file_type_label.move(10, 40)
        self.file_type_label.setFixedWidth(80)
        self.file_type_label.setFixedHeight(30)
        self.file_type_label.setAlignment(Qt.AlignCenter)
        self.file_type_label.setText("文件类型 : ")
        self.show()

    '''label文本修改'''
    def on_file_type_label(self,text):
        self.file_type_label.setText("文件类型 : " + text)

    """创建鼠标点击事件"""
    @pyqtSlot()
    def on_file_click(self):
        self.setBrowerPath()

    """创建鼠标点击事件"""
    @pyqtSlot()
    def on_ack_click(self):
        FileData.instance().initdata(filename=self.file_path)

    # @pyqtSlot()
    # def on_text_change(self):
    #     fileData.initdata(filename=self.file_path)



